//
//  HDBankInformationVC.swift
//  HuaDaoPeifu
//
//  Created by liangyuanliangyuan on 16/5/27.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit
import SVProgressHUD
class HDBankInformationVC: UIViewController, UITableViewDelegate, UITableViewDataSource{
    
    var bankArray = [HDUserBankInfo]()
    var tableView: UITableView?
    var cell: HDBankInformationCell?
    var cellBankHeight: CGFloat = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(hex: 0xfcfcfc)
        title = "银行信息"
        setupTableView()
        loadbankInfo()
        //tableView!.reloadData()
    }
    
    func setupTableView() {
        tableView = UITableView(frame: CGRect.zero, style: .plain)
        tableView?.separatorStyle = .none
        tableView?.delegate = self
        tableView?.dataSource = self
        self.view.addSubview(tableView!)
        let edge = UIEdgeInsetsMake(0, 0, 0, 0)
        tableView?.snp.makeConstraints({ (make) in
            make.edges.equalTo(edge)
        })
    }
    
    func setupNoCardView() {
        tableView!.removeFromSuperview()
        let noInfoView = HDNoBankInfoView(frame: CGRect.zero)
        noInfoView.backgroundColor = UIColor.red
        self.view.addSubview(noInfoView)
        noInfoView.snp.makeConstraints({ (make) in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.centerY.equalTo(self.view.snp.centerY).offset(-100)
        })
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return bankArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        //        return 111+(cell?.bankOpenheight)!
        let heighttt = 111 + cellBankHeight
        return heighttt
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        cell = HDBankInformationCell(data: bankArray[indexPath.row], style: .default, reuseIdentifier: "HDBankInformationCell")
        cell!.selectionStyle = .none
        cellBankHeight = (cell?.bankOpenheight)!
        return cell!
    }
    
    func loadbankInfo() {
        
        HDApiManagerHandler.requestBankInfo { (result:HDApiResult?) in
            guard let result = result else {
                self.setupNoCardView()
                return
            }
            
            
            if (result.status == .Success){
                guard let banks = NSArray.yy_modelArray(with: HDUserBankInfo.self, json: result.data!) else {
                    self.setupNoCardView()
                    return
                }
                self.bankArray = banks as! [HDUserBankInfo]
                self.tableView!.reloadData()
            }else{
                SVProgressHUD.showError(result.msg)
            }
        }
        
        
//        HDApiManagerHandler.requestBankInfo({ [unowned self](bankInfo) in
//            print(bankInfo)
//            if bankInfo["status"]?.int32Value == 1 {
//                let modelArray = bankInfo["data"] as! NSArray
//                self.bankArray = PYPBankInfo.parses(modelArray) as! [PYPBankInfo]
//                if self.bankArray.count == 0 {
//                    self.setupNoCardView()
//                }
//                self.tableView!.reloadData()
//            }else {
//                let msg = bankInfo["msg"] as! String
//                SVProgressHUD.showError(msg)
//            }
//        }) { (error) in
//
//        }
    }
    
}







